package com.qf.bookmanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.bookmanage.entity.Book;
import com.qf.bookmanage.mapper.BookMapper;
import com.qf.bookmanage.service.IBookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dongjl
 * @since 2023-11-23
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements IBookService {

    @Autowired
    private BookMapper bookMapper;

    @Override
    public Book addBook(Book book) {

        // 判断书名和作者都一样就是重复了
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.eq("name",book.getBookname());
        wrapper.eq("author",book.getAuthor());
        Long count = bookMapper.selectCount(wrapper);
        if (count>0){
            System.out.println("重复了："+book);
            throw new RuntimeException("书本重复了");
        }

        // 不重复才把book添加到数据库中
        bookMapper.insert(book);
        System.out.println("插入书籍成功了："+book);
        return book;
    }
}
